/**
 * 文件名：@StringUtil.java <br/>
 * 包名：cn.efunbox.afw.core.util <br/>
 * 项目名：afw-core <br/>
 * @author xtwin <br/>
 */
package cn.efunbox.afw.core.util;

import java.io.UnsupportedEncodingException;

/**
 * 类名：StringUtil  <br />
 *
 * 功能：字符串工具类
 *
 * @author xtwin <br />
 * 创建时间：2015年12月2日 下午3:04:51  <br />
 * @version 2015年12月2日
 */
public class StringUtil {

	
	/**
	 * 功能：阶梯式打包 <br/>
	 * 	
	 * 	如：stepUnpick("1,2,3,4,5,6", ",", "|")，会得出如下结果
	 * 		1
	 * 		1|2
	 * 		1|2|3
	 * 		1|2|3|4
	 * 		1|2|3|4|5
	 * 		1|2|3|4|5|6
	 *
	 * @author xtwin <br/>
	 * @version 2015-7-20 上午10:46:20 <br/>
	 */
	public static String[] stepUnpick(String text, String split, String separator) {
		String[] values = text.split(split);
		
		for (int i = 1, len = values.length; i < len; i++) {
			values[i] = values[i - 1] + separator + values[i];
		}
		
		return values;
	}
	
	/**
	 * 
	 * 功能：若参数为null或则为全是空格的字符串，则返回null，否则返回原值 <br/>
	 *
	 * @author xtwin <br/>
	 * @version 2014-2-17 上午11:16:59 <br/>
	 */
	public static String emptyAdjust(String src) {
		return emptyAdjust(src, null);
	}
	
	/**
	 * 
	 * 功能：若第一个参数为null或则为全是空格的字符串，则返回第二个参数的值，否则返回第一个参数的值 <br/>
	 *
	 * @author xtwin <br/>
	 * @version 2014-2-17 上午11:16:23 <br/>
	 */
	public static String emptyAdjust(String src, String _default) {
		if (null == src || "".equals(src.trim())) {
			src = _default;
		}
		
		return src;
	}
	
	/**
	 * 
	 * 功能：若字符串为null或为全是空格的字符串则返回true，否则返回false <br/>
	 *
	 * @author xtwin <br/>
	 * @version 2013-12-7 下午03:11:04 <br/>
	 */
	public static boolean isEmpty(String text) {
		return null == text || text.trim().length() == 0;
	}

	/**
	 * 
	 * 功能：提取一组汉字拼音的首字母，结果为小写 <br/>
	 *
	 * @author xtwin <br/>
	 * @version 2013-11-15 上午09:25:10 <br/>
	 */
	public static String getPinym(String str) throws UnsupportedEncodingException {
		// 汉字区位码
		int li_SecPosValue[] = { 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 
				3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 };
		
		// 存放国标一级汉字不同读音的起始区位码对应读音 23
		char lc_FirstLetter[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 
				'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z' };
		
		//存放所有国标二级汉字读音
		String ls_SecondSecTable = "CJWGNSPGCGNE[Y[BTYYZDXYKYGT[JNNJQMBSGZSCYJSYY"
				+ "[PGKBZGY[YWJKGKLJYWKPJQHY[W[DZLSGMRYPYWWCCKZNKYYGTTNJJNYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGKJFYXJWZLTBNCXJJJJTXDTTSQZYCDXXHGCK"
				+ "[PHFFSS[YBGXLPPBYLL[HLXS[ZM[JHSOJNGHDZQYKLGJHSGQZHXQGKEZZWYSCSCJXYEYXADZPMDSSMZJZQJYZC[J"
				+ "[WQJBYZPXGZNZCPWHKXHQKMWFBPBYDTJZZKQHYLYGXFPTYJYYZPSZLFCHMQSHGMXXSXJ["
				+ "[DCSBBQBEFSJYHXWGZKPYLQBGLDLCCTNMAYDDKSSNGYCSGXLYZAYBNPTSDKDYLHGYMYLCXPY"
				+ "[JNDQJWXQXFYYFJLEJPZRXCCQWQQSBNKYMGPLBMJRQCFLNYMYQMSQYRBCJTHZTQFRXQHXMJJCJLXQGJMSHZKBSWYEMYLTXFSYDSWLYCJQXSJNQBSCTYHBFTDCYZDJWY"
				+ "GHQFRXWCKQKXEBPTLPXJZSRMEBWHJLBJSLYYSMDXLCLQKXLHXJRZJMFQHXHWYWSBHTRXXGLHQHFNM[YKLDYXZPYLGG[MTCFPAJJZYLJTYANJGBJPLQGDZY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XYQZJSGPSXQZQZRQTBDKYXZKHHGFLBCSMDLDGDZDBLZYYCXNNCSYBZBFGLZZXSWMSCCMQNJQSBDQSJTXXMBLTXZCLZSHZCXRQ"
				+ "JGJYLXZFJPHYMZQQYDFQJJLZZNZJCDGZYGCTXMZYSCTLKPHTXHTLBJXJLXSCDQXCBBTJFQZFSLTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPH"
				+ "JSGZGSZZQLYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQFHBSAQTGYLBXMMYGSZLDYDQMJJRGBJ"
				+ "TKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ";

		StringBuilder _pym = new StringBuilder();
		//int len = str.length();
		//for (int j = 0; j < len; j++) {
		for (char c : str.toCharArray()) {
			/*String stemp = str.substring(j, j + 1);
			byte[] by = stemp.getBytes("GBK");*/
			byte[] by = ("" + c).getBytes("GB2312");
			if (by.length == 1) {
				_pym.append(c);
			} else {
				int ia = 96 + (int) by[0]; // 区码
				int ib = 96 + (int) by[1]; // 位码
				int in = ia * 100 + ib;
				if (in > 1600 && in < 5590) {
					for (int i = 0; i < 24; i++) {
						if (in < li_SecPosValue[i]) {
							_pym.append(lc_FirstLetter[i - 1]);
							break;
						}
					}
				} else {
					//TODO xtwin 二级字库处理，不正确，待解决
					int ioffset = (ia - 56) * 94 + ib - 1;
					if (ioffset >= 0 && ioffset <= 3007) {
						_pym.append(ls_SecondSecTable.substring(ioffset, ioffset + 1));
					}
				}
			}
		}
		return _pym.toString().toLowerCase();
	}
}
